<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-card>
        <div slot="header" class="clearfix" style="height:10px;margin-top:-10px;">
          <span style="font-weight:bold;color:#1890FF;font-size:14px;">查询条件设置</span>
        </div>
        <el-form-item label="名称" prop="name">
          <el-input
            v-model="queryParams.name"
            placeholder="请输入数据集名称"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item label="编码" prop="code">
          <el-input
            v-model="queryParams.code"
            placeholder="编码"
            clearable
            @keyup.enter.native="handleQuery"
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
          <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-card>
    </el-form>

    <el-card style="margin-top:10px;">
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button
            type="primary"
            plain
            icon="el-icon-plus"
            size="mini"
            @click="handleAdd"
          >新增</el-button>
        </el-col>
        <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
      </el-row>

      <el-table v-loading="loading" border :data="datasetList" @selection-change="handleSelectionChange">
        <el-table-column label="名称" align="center" prop="name">
          <template slot-scope="scope">
            <span class="link-type" style="cursor:pointer;" @click="editDatasetCata(scope.row)" title="点击进入数据集数据目录配置">{{ scope.row.name }}</span>
          </template>
        </el-table-column>
        <el-table-column label="编码" align="center" prop="code">
          <template slot-scope="scope">
            <span class="link-type" style="cursor:pointer;" @click="editDatasetCata(scope.row)" title="点击进入数据集数据目录配置">{{ scope.row.code }}</span>
          </template>
        </el-table-column>
        <el-table-column label="状态" align="center" prop="status" width="80">
          <template slot-scope="scope">
            <!-- <dict-tag :options="dict.type.sys_dataset_status" :value="scope.row.status"/> -->
            <span v-if="scope.row.status==1" style="color:green">已发布</span>
            <span v-if="scope.row.status==2" style="color:blue">未发布</span>
          </template>
        </el-table-column>
        <el-table-column label="创建人" align="center" prop="creator" width="80" />
        <el-table-column label="部门名称" align="center" prop="deptName" width="200"/>
        <el-table-column label="创建日期" align="center" prop="createTime" width="120">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center" width="200">
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit-outline"
              @click="editDatasetCata(scope.row)"
            >目录配置</el-button>
            <el-button
              size="mini"
              type="text"
              icon="el-icon-edit-outline"
              @click="handleUpdate(scope.row)"
            >修改</el-button>
            <el-button
              size="mini"
              type="text"
              :disabled="scope.row.status==1"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
            >删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
    </el-card>

    <!-- 添加或修改数据集信息表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="50%" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="60px">
        <el-card style="margin-top:-30px;">
          <div slot="header" class="clearfix">
            <span style="font-weight:bold;color:#1890FF;font-size:14px;">新建数据集</span>
          </div>
          <el-form-item label="名称" prop="name">
            <el-input v-model="form.name" @blur="checkNameExist" placeholder="请输入数据集名称" />
          </el-form-item>
          <el-form-item label="编码" prop="code">
            <el-input v-model="form.code" disabled/>
          </el-form-item>
          <el-form-item label="状态" prop="status">
            <el-radio-group v-model="form.status">
              <el-radio v-for="dict in dict.type.sys_dataset_status" :key="dict.value" :label="parseInt(dict.value)" border>{{dict.label}}</el-radio>
            </el-radio-group>
          </el-form-item>
          <el-form-item label="说明" prop="remark">
            <el-input v-model="form.remark" type="textarea" rows="10" placeholder="请输入内容" />
          </el-form-item>
        </el-card>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDataset, getDataset, delDataset, addDataset, updateDataset,getCode } from "@/api/system/datasetinfo";

export default {
  name: "Dataset",
  dicts: ['sys_dataset_status', 'sy_catagory_type', 'sy_catagory_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 数据集信息表表格数据
      datasetList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        name: null,
        code: null,
        status: null,
        deptId: null,
        deptName: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "数据集名称不能为空", trigger: "blur" }
        ],
        code: [
          { required: true, message: "数据集编码不能为空", trigger: "blur" }
        ],
        status: [
          { required: true, message: "请选择数据集状态", trigger: "change" }
        ],
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    editDatasetCata(row){
      this.$router.push({
        path: "/datasetcatagory",
        query: {
          dataset:row
        }
      });
    },
    checkNameExist(){
      getCode(this.form).then(res =>{
        if(res.code===200){
          this.form.code = res.data.code;
        }else{
          this.form.code = null;
          this.$message.error(res.msg)
        }
      })
    },
    /** 查询数据集信息表列表 */
    getList() {
      this.loading = true;
      listDataset(this.queryParams).then(response => {
        this.datasetList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        uuid: null,
        name: null,
        code: null,
        status: 1,
        deptId: null,
        deptName: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.uuid)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "新建数据集";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const uuid = row.uuid || this.ids
      getDataset(uuid).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改数据集";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.uuid != null) {
            updateDataset(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addDataset(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const uuids = row.uuid || this.ids;
      this.$modal.confirm('是否确认删除数据集？').then(function() {
        return delDataset(uuids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/dataset/export', {
        ...this.queryParams
      }, `dataset_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
